﻿using UnityEngine;
using System.Collections;
using System;

public class GUIHelper
{
    public static void PrintShadowedText(string text, Rect rect)
    {
        GUI.color = Color.black;
        GUI.Label(new Rect(rect.x + 1, rect.y + 1, rect.width, rect.height), text);
        GUI.color = Color.white;
        GUI.Label(rect, text);
    }

    public static void PrintScrollText(string text, Rect rect, ref Vector2 scrollPosition)
    {
        GUILayout.BeginArea(rect);
        scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Width(rect.width), GUILayout.Height(rect.height));

        // Changes mode 
        GUI.skin.scrollView.alignment = TextAnchor.UpperLeft;

        // Set the word wrap on for box only.
        GUI.skin.box.wordWrap = true;
        GUI.skin.box.alignment = TextAnchor.MiddleLeft;

        GUI.skin.verticalScrollbar.fixedWidth = 30f;
        GUI.skin.verticalScrollbarThumb.fixedWidth = 30f;

        // Just your message as parameter.
        GUILayout.Box(text);                       

        GUILayout.EndScrollView();
        GUILayout.EndArea();
    }

    public static Rect RatioRect(float x, float y, float widget, float heiht)
    {
        return new Rect(Screen.width * x, Screen.height * y, Screen.width * widget, Screen.height * heiht);
    }

    public static Rect LayoutIndexRect(int index, int countPerRow, float itemRWidget, float itemRHeight, float startRX, float startRY)
    {
        // Prepare variables for calculation
        int padding = 5;
        int startX = (int)(Screen.width * startRX);
        int startY = (int)(Screen.height * startRY);
        int w = (int)(Screen.width * itemRWidget);
        int h = (int)(Screen.height * itemRHeight);

        // Calculate position
        int x = startX + (index % countPerRow) * (w + padding);

        int row = (int)(index / countPerRow);
        int y = startY + row * (h + padding);

        return new Rect(x, y, w, h);
    }

    public static void DrawButton(string label, Action onClick, Color color, float width = -1)
    {
        GUI.backgroundColor = color;

        if (width == -1)
        {
            if (GUILayout.Button(label))
            {
                onClick();
            }
        }
        else
        {
            if (GUILayout.Button(label, GUILayout.Width(width)))
            {
                onClick();
            }
        }

        GUI.backgroundColor = Color.white;
    }


}
